﻿using AutoUpgrade.Win32API;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoUpgrade
{
    /// <summary>
    /// 带阴影的无边框窗口
    /// </summary>
    internal partial class SkinForm : System.Windows.Forms.Form
    {



        private bool m_aeroEnabled;                     // variables for box shadow

        /// <summary>
        /// 构建 带阴影的无边框窗口 实例。
        /// </summary>
        public SkinForm()
        {
            InitializeComponent();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                m_aeroEnabled = CheckAeroEnabled();
                CreateParams cp = base.CreateParams;
                if (!m_aeroEnabled)
                    cp.ClassStyle |= MessageEnum.CS_DROPSHADOW;
                return cp;
            }
        }

        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                Win32API.DwmApi.DwmIsCompositionEnabled(ref enabled);
                return (enabled == 1);
            }
            return false;
        }

        protected override void WndProc(ref Message m)
        {
            if (!this.DesignMode)
            {
                switch (m.Msg)
                {
                    // 处理无边框中的阴影效果
                    case MessageEnum.WM_NCPAINT:
                        if (m_aeroEnabled)
                        {
                            var v = 2;
                            Win32API.DwmApi.DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                            Win32API.MARGINS margins = new MARGINS()
                            {
                                bottomHeight = 1,
                                leftWidth = 1,
                                rightWidth = 1,
                                topHeight = 1
                            };
                            Win32API.DwmApi.DwmExtendFrameIntoClientArea(this.Handle, ref margins);
                        }
                        break;
                    default:
                        break;
                }
            }
            base.WndProc(ref m);
        }
    }
}
